<template>

  <el-aside  :width="main.isCollapse ? '170px': '60px'"  >

    <el-menu default-active="2" height="100%" class="el-menu-vertical-demo" background-color="#4a4e52" text-color="#fff
    "  :collapse="!main.isCollapse" :collapse-transition="false">
        <h3 v-show="main.isCollapse">VUE后台</h3>
        <h3 v-show="!main.isCollapse">VUE</h3>

      <el-menu-item @click="clickMenu(item)" v-for="item in noChildren()" :key="item.name" :index="item.label"  >
        <component class="icons" :is="item.icon"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>


      <el-sub-menu :index="item.label" v-for="item in hasChildren()" :key="item.path">
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item v-for="(subItem, subIdex) in item.children" :index="subItem.path" :key="subIdex">
            <component class="icons" :is="subItem.icon"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>

    </el-menu>

  </el-aside>
</template>


<script setup>
// 引入仓库
import router from '@/router';
import { useAside } from '@/store/index.js';
const main=useAside()


const list = [
  {
    path: '/',
    name: 'home',
    label: '首页',
    icon: 'house',
    url: 'Home/Home'
  },
  {
    path: '/mall',
    name: 'mall',
    label: '商品管理',
    icon: 'video-play',
    url: 'MallManage/MallManage'
  },
  {
    path: '/user',
    name: 'user',
    label: '用户管理',
    icon: 'user',
    url: 'UserManage/UserManage'
  },
  {
    label: '其他',
    icon: 'location',
    path: '/outher',
    children: [
      {
        path: '/page1',
        name: 'page1',
        label: '页面1',
        icon: 'setting',
        url: 'Other/PageOne'
      },
      {
        path: '/page2',
        name: 'page2',
        label: '页面2',
        icon: 'setting',
        url: 'Other/PageTwo'
      }
    ]
  }
]


const noChildren = () => {
  return list.filter((item) => !item.children)
}
const hasChildren = () => {
  return list.filter((item) => item.children)
}

const handleSelectMenu=(val)=>{
  main.selectMenu(val)
}

// console.log(list.filter((item)=> item.children));
// 点击侧边栏实现跳转
const clickMenu =(item)=>{
  // console.log(item);
  router.push({
    name:item.name
  })
  handleSelectMenu(item)
}



</script>

<style lang="less">
.icons {
  width: 20px;
  height: 20px;
}

.el-aside {
  height: 100%;
}

.el-menu {
  border: none;

  h3{
    font-weight: 800;
    color: #fff;
    text-align: center;
    line-height: 48px;
  }
}
</style>
